/*
 * Copyright 2009, Qys Creations 
 * 
 *  This file is part of Chameleon DB.
 *
 *  Chameleon DB is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Chameleon DB is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Chameleon DB.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * */


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class db_util_parser {
	public String sQuery = "";
	public String sValue = "";
	private String[] oTokens = null;
	
	db_util_parser (String[] oTokenList){
		oTokens = oTokenList;
	}
	
	public void restart(String sQueryStr) {
		sValue = "";
		sQuery = sQueryStr;
	}
	
	public  boolean matchReplaceNoSpace(String sRep, String sComp) {
	
		if (sRep.toUpperCase().startsWith(sComp)) {
		
			sQuery = sRep.substring(sComp.length(), sRep.length()).trim();
			sValue = sComp;
			return true;
		}
		return false;
	}

	public  boolean matchReplace(String sRep, String sComp) {
		
		if (sRep.toUpperCase().startsWith(sComp + " ")) {
			sQuery = sRep.substring(sComp.length(), sRep.length()).trim();
			sValue = sComp;
			return true;
		}

		return false;
	}
	public boolean matchReplace(String sRep, Pattern sComp) {
		
		// Very very very bad for performance
		for (int i = 0; i < oTokens.length; i++)
			if (sQuery.startsWith(oTokens[i] + " "))
				return false;
		
		Matcher ma = sComp.matcher(sRep);
		if (ma.find() && ma.start() == 0) {
			
			sValue = ma.group();
			sQuery = sRep.substring(ma.group().length(), sRep.length()).trim();
			return true;
		}
		return false;
	}	
}
